﻿using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using UnityEngine;
using System.Text;

public class _G
{
    private static long beginTime;

    public static void Print(string str)
    {
        Debug.Log(str);
        if (Config.DEBUG == 1)
        {
            string fileName = Convert.ToString("Logs/" + beginTime + ".log");
            FileStream fs = new FileStream(fileName, FileMode.Append);
            StreamWriter writer = new StreamWriter(fs);
            writer.Write(DateTime.Now + "\t" + str + "\n");
            writer.Close();
            fs.Close();
        }
    }

    public static void CreateLogFile()
    {
        beginTime = DateTime.Now.Ticks;
        if (Directory.Exists("Logs") == false)
        {
            Directory.CreateDirectory("Logs");
        }
        Print("Start game log in " + DateTime.Now);
    }

    public static string CreateUrl(string _query,string host)
    {
        string _url = null;
        if (host == null)
        {
            _url = "http://" + Config.ipAddress + ":" + Config.portID + "/" + _query;
        }
        else
        {
            _url = "http://" + host + "/" + _query;
        }
        return _url;
    }

    public static string GetSignByOpenId(string openId)
    {
        string sign = MD5_HASH_STRING(openId);
        string newText = sign.Substring(0, 10) + Config.signKey;
        string retSign = MD5_HASH_STRING(newText);
        return retSign;
    }

    public static string MD5_HASH_STRING(string src)
    {
        MD5 md5 = System.Security.Cryptography.MD5.Create();
        byte[] _bytes = System.Text.Encoding.UTF8.GetBytes(src);
        byte[] hash = md5.ComputeHash(_bytes);
        string dstStr = "";
        for (int i = 0; i < hash.Length; i++)
        {
            dstStr += hash[i].ToString("x2");
        }
        return dstStr;
    }
}
